//
//  WPNewCommentCell.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/24.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewCommentCell.h"
#import "WPNewCommentModel.h"

@interface WPNewCommentCell ()

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UIButton *praiseBtn;
@property(nonatomic,strong)UILabel *praiseCount;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UIImageView *pointImageView;
@property(nonatomic,strong)UILabel *replyLabel;
@property(nonatomic,strong)UIImageView *enterTheCommentImageView;
@property(nonatomic,assign)BOOL isShow;

@end
@implementation WPNewCommentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = RGB_SAME(245);
        [self setupUI];
        
    }
    return self;
}

-(void)setupUI
{
    [self iconView];
    [self nameLabel];
    [self contentLabel];
    [self timeLabel];
    [self pointImageView];
    [self replyLabel];
    [self enterTheCommentImageView];
    [self praiseBtn];
    [self praiseCount];
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        UIImage *image = [UIImage imageNamed:@"comment-default_icon"];
        _iconView = [[UIImageView alloc] initWithImage:image];
        [_iconView sizeToFit];
        
        _iconView.layer.cornerRadius = image.size.width*0.5;
        _iconView.layer.masksToBounds = YES;
        
        [self.contentView addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.top.equalTo(self.contentView).offset(20);
            make.width.height.mas_equalTo(image.size.width);

        }];
    }
    return _iconView;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = RGB_SAME(102);
        _nameLabel.font = [UIFont systemFontOfSize:15];
        [_nameLabel sizeToFit];
//        _nameLabel.text = self.model.userName;
        [self.contentView addSubview:_nameLabel];
        
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView);
            make.left.equalTo(self.iconView.mas_right).offset(15);
        }];
    }
    return _nameLabel;
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil)
    {
        _contentLabel = [UILabel new];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = GLobalFontColor;
//        _contentLabel.text = self.model.content;
        [self.contentView addSubview:_contentLabel];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
            make.right.equalTo(self.contentView).offset(-15);
        }];
    }
    return _contentLabel;
}

-(UILabel *)timeLabel
{
    if (_timeLabel == nil)
    {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGB_SAME(102);
//        _timeLabel.backgroundColor = [UIColor orangeColor];
        [_timeLabel sizeToFit];
        [self.contentView addSubview:_timeLabel];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentLabel);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        }];
        
    }
    return _timeLabel;
}

-(UIImageView *)pointImageView
{
    if (_pointImageView == nil)
    {
        _pointImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"point_icon"]];
        [self.contentView addSubview:_pointImageView];
        
        [_pointImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.left.equalTo(self.timeLabel.mas_right).offset(5);
        }];
    }
    return _pointImageView;
}

-(UILabel *)replyLabel
{
    if (_replyLabel == nil )
    {
        _replyLabel = [UILabel new];
        _replyLabel.font = [UIFont systemFontOfSize:12];
        _replyLabel.textColor = RGB_SAME(102);
        [_replyLabel sizeToFit];
        [self.contentView addSubview:_replyLabel];
        
        [_replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.left.equalTo(self.pointImageView.mas_right).offset(5);
        }];
        
    }
    return _replyLabel;
}

-(UIImageView *)enterTheCommentImageView
{
    if (_enterTheCommentImageView == nil)
    {
        _enterTheCommentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"enterTheComment_icon"]];
        [self.contentView addSubview:_enterTheCommentImageView];
        
        [_enterTheCommentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.left.equalTo(self.replyLabel.mas_right).offset(8);
        }];
    }
    return _enterTheCommentImageView;
}

-(UILabel *)praiseCount
{
    if (_praiseCount == nil)
    {
        _praiseCount = [UILabel new];
        _praiseCount.textColor = RGB_SAME(183);
        _praiseCount.font = [UIFont systemFontOfSize:14];
        
        _praiseCount.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_praiseCount];
        
        [_praiseCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.contentView).offset(-15);
        }];
    }
    return _praiseCount;
}

-(UIButton *)praiseBtn
{
    if (_praiseBtn == nil)
    {
        _praiseBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_normal"] forState:UIControlStateNormal];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_selected"] forState:UIControlStateSelected];
        [_praiseBtn addTarget:self action:@selector(praiseBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.contentView addSubview:_praiseBtn];
        
        [_praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.praiseCount.mas_left).offset(-5);
            make.bottom.equalTo(self.contentView).offset(-20);
        }];
    }
    return _praiseBtn;
}


-(void)setModel:(WPNewCommentModel *)model
{
    _model = model;
    
    self.nameLabel.text = model.userName;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.userImage] placeholderImage:[UIImage imageNamed:@"comment-default_icon"]];
    self.contentLabel.text = model.content;
    self.timeLabel.text = [[WPDateFormatterManager sharedManager] wbwyTimeStrDescriptionWithTimeStampStr:[NSString stringWithFormat:@"%zd",model.time]];
    
    if (self.isSubAppraise)
    {
        
       self.enterTheCommentImageView.hidden = YES;
        
        if (self.enableReply)
        {
            self.replyLabel.hidden = NO;
            self.replyLabel.text = @"回复";
            self.pointImageView.hidden = NO;
        }
        else
        {
            self.replyLabel.hidden = YES;
            self.pointImageView.hidden = YES;
        }
    }
    else
    {
        self.pointImageView.hidden = NO;
        self.replyLabel.hidden = NO;
        self.enterTheCommentImageView.hidden = NO;
        self.replyLabel.text = [NSString stringWithFormat:@"%zd条回复",model.replyAmount];
    }
    self.praiseCount.text = [NSString stringWithFormat:@"%zd",model.praiseAmount];
}

-(void)praiseBtnDidClick:(UIButton *)btn
{
    if (btn.selected)
    {
        //        弹穿提示已经赞过
        [self showMessageLabelWith:@"亲, 已经赞过了~" font:[UIFont systemFontOfSize:13]];
        return ;
    }
    
    btn.selected = !btn.selected;
    self.model.isPraised = YES;
    self.model.praiseAmount++;
    self.praiseCount.text = [NSString stringWithFormat:@"%zd",self.model.praiseAmount];
    
    //    把评论的id传给用户
    NSString *url = @"appraise/praise.do";
    NSDictionary *dict = @{@"appraiseId":@(self.model.id)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//                NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

-(void)showMessageLabelWith:(NSString *)title font:(UIFont *)font
{
    if (self.isShow)
    {
        return ;
    }
    UIButton *messageBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    [messageBtn setTitle:title forState:UIControlStateNormal];
    messageBtn.titleLabel.font = font;
    messageBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.6);
    messageBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [messageBtn sizeToFit];
    messageBtn.layer.cornerRadius = 3;
    messageBtn.layer.masksToBounds = YES;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:messageBtn];
    self.isShow = YES;
    messageBtn.center = CGPointMake(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.4);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:1 animations:^{
            
            messageBtn.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            [messageBtn removeFromSuperview];
            self.isShow = NO;
        }];
        
    });
}

@end
